package org.chen.hotel.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.chen.common.hotel.vo.CityVO;
import org.chen.common.hotel.vo.ConvenientAttractionVO;
import org.chen.common.result.ResultVO;
import org.chen.hotel.service.CityService;
import org.chen.hotel.service.ConvenientAttractionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Tag(name = "城市和景点接口", description = "城市和便捷景点相关接口")
public class CityAttractionController {
    
    @Autowired
    private CityService cityService;
    
    @Autowired
    private ConvenientAttractionService attractionService;
    
    /**
     * 获取所有启用的城市
     */
    @GetMapping("/cities")
    @Operation(summary = "获取所有城市", description = "获取所有启用的城市列表")
    public ResultVO<List<CityVO>> getAllCities() {
        List<CityVO> cities = cityService.getAllActiveCities();
        return ResultVO.success(cities);
    }
    
    /**
     * 根据国家ID获取城市列表
     */
    @GetMapping("/cities/country/{countryId}")
    @Operation(summary = "根据国家获取城市", description = "根据国家ID获取该国家的城市列表")
    public ResultVO<List<CityVO>> getCitiesByCountryId(
            @PathVariable @Parameter(description = "国家ID") Integer countryId) {
        List<CityVO> cities = cityService.getCitiesByCountryId(countryId);
        return ResultVO.success(cities);
    }
    
    /**
     * 根据城市代码获取城市信息
     */
    @GetMapping("/cities/code/{code}")
    @Operation(summary = "根据代码获取城市", description = "根据城市代码获取城市信息")
    public ResultVO<CityVO> getCityByCode(
            @PathVariable @Parameter(description = "城市代码") String code) {
        CityVO city = cityService.getCityByCode(code);
        return city != null ? ResultVO.success(city) : ResultVO.fail("城市不存在");
    }
    
    /**
     * 获取所有便捷景点
     */
    @GetMapping("/attractions/convenient")
    @Operation(summary = "获取所有便捷景点", description = "获取所有启用的便捷景点列表")
    public ResultVO<List<ConvenientAttractionVO>> getAllConvenientAttractions() {
        List<ConvenientAttractionVO> attractions = attractionService.getAllActiveAttractions();
        return ResultVO.success(attractions);
    }
    
    /**
     * 根据城市ID获取便捷景点
     */
    @GetMapping("/attractions/convenient/city/{cityId}")
    @Operation(summary = "根据城市获取便捷景点", description = "根据城市ID获取该城市的便捷景点")
    public ResultVO<List<ConvenientAttractionVO>> getAttractionsByCityId(
            @PathVariable @Parameter(description = "城市ID") Integer cityId) {
        List<ConvenientAttractionVO> attractions = attractionService.getAttractionsByCityId(cityId);
        return ResultVO.success(attractions);
    }
    
    /**
     * 根据景点名称模糊查询
     */
    @GetMapping("/attractions/convenient/search")
    @Operation(summary = "搜索便捷景点", description = "根据名称模糊查询便捷景点")
    public ResultVO<List<ConvenientAttractionVO>> searchAttractions(
            @RequestParam @Parameter(description = "景点名称关键词") String name) {
        List<ConvenientAttractionVO> attractions = attractionService.getAttractionsByNameLike(name);
        return ResultVO.success(attractions);
    }
    
    /**
     * 获取城市的景点数量
     */
    @GetMapping("/attractions/convenient/city/{cityId}/count")
    @Operation(summary = "获取城市景点数量", description = "获取指定城市的便捷景点数量")
    public ResultVO<Long> getAttractionCountByCityId(
            @PathVariable @Parameter(description = "城市ID") Integer cityId) {
        long count = attractionService.getAttractionCountByCityId(cityId);
        return ResultVO.success(count);
    }
}